<?

	require_once '_rootdir.php';
	
	class Fmgr0v0r01_Desk extends Fmgr0v0r01__rootdir{

		protected $rdk = 'desk';
		protected $mod = 775;
		
		public $media_class = 'Desktop';
		protected $udnames = array('Scripts','Trash','Users');
		protected $ufnames = array('Docroot','Home');
		
		// set during check_sanity and acted on during post_commit
		private $update_domain_perms;
		
####	Bootstrapping
		
		function bootstrap($rf=ARF){
			$req = smuoi_explode('/', $rf);
			$app = $this->application(@$req[0]);
			$args = $app->arguments();
			// Is it the desktop? 
			$is_this = ($app === $this);
			// Simultaneously gets args->href and shift request array for args->rf
			$args->href =  '/' . (($is_this) ? '' : array_shift($req));
			// args->is_app_base : bool we are at the base of the application. args->rf : request file beneath the app
			// the desktop doesn't really consider itself an application, so false if it's ourself
			$args->is_app_base = ((!$args->rf = implode('/', $req)) && !$is_this);
			// Check the context and include accordingly
			if(substr($args->modpath, 0, 1) == '@'){
				if(!$modpath = sn()->package_path(substr($args->modpath, 1), $args->domain)) $this->e500($app, 'modpath', $args->modpath);
				$args->modpath = $modpath;
			}
			$file = PKG_DIR."/".$args->modpath."/Interface.php";
			if(!file_exists($file)) $this->e500($app, 'modpath', $args->modpath);
			require_once $file;
			// Instantiate the class
			$modclass = 'Interface_'.alphanum($args->modpath);
			$APP = new $modclass($args);
			return $APP;
		}
		
		function e500($app, $k, $v){
			httpE(500, 'Loading application '.$app->name." with $k '$v' totally didn't happen.");
		}
		
		function arguments(){
			$dom_id = $this->pkg->dom_id();
			return new args(array(
				'modpath'=>'@fmgr',
				'icon' => 'Desk.png',
				'fmrp'=>$dom_id.'::desk:/'
			));
		}
		
		// application returns the object based on the first argument of request_filename, or $this on failure
		function application($name=false){
			if(!$name) return $this;
			if(($app = $this->child($name)) && ($this->pkg->is_fmgr_app($app)) && $app->is_executable()) return $app;
			return (substr($name, 0, 1) == '@') ? new args('modpath', $name) : $this;
		}
		
####	Filenames with a preceding @ sign are reserved	
		
		function permit_child_basename($b){
			while(substr($b, 0, 1) == '@') $b = substr($b, 1);
			return parent::permit_child_basename($b);
		}
		
#####	
		
		protected function run_sanity_checks(){
			parent::run_sanity_checks();
			if($this->is_edited('own') || $this->is_edited('grp')) $this->update_domain_perms = true;	
		}
		
		protected function post_commit(){
			parent::post_commit();
			//if($this->update_domain_perms){
				$dom = $this->pkg->domain();
				if($dom->own !== $this->data->own) $dom->own = smdb($this->data->own);
				if($dom->grp !== $this->data->grp) $dom->grp = smdb($this->data->grp);
				$dom->commit(1);
				$tmp = $this->pkg->open('temp:/');
				if($tmp->own !== $this->data->own) $tmp->own = smdb($this->data->own);
				if($tmp->grp !== $this->data->grp) $tmp->grp = smdb($this->data->grp);
				$tmp->mode_recurse = 1;
				$tmp->commit(1);
				$this->update_domain_perms = false;	
			//}
		}
		
	}

?>